<template>
   <div class="add-dictionary" >
   		<el-dialog :title="title" :visible.sync="catalogFormVisible" :before-close="addClose" width="25%" align="left">
          <el-form ref="form" label-position="right" label-width="80px" :rules="rules" :model="treeForm">
              <el-form-item label="目录名称" prop="attrName">
                <el-input v-model="treeForm.attrName" ></el-input>
              </el-form-item>
              <el-form-item label="唯一标识" prop="attrId">
                <el-input v-model="treeForm.attrId" ></el-input>
              </el-form-item>
              <el-form-item label="是否目錄" prop="isParent">
                  <el-radio v-model="treeForm.isParent" :label="1">是</el-radio>
                  <el-radio v-model="treeForm.isParent" :label="0">否</el-radio>
              </el-form-item>
              <el-form-item label="排序" prop="attrOrderId">
                <el-input type="number" v-model="treeForm.attrOrderId" ></el-input>
              </el-form-item>
              <div style="padding-bottom:10px;text-align:right">
               <el-button type="primary" @click="dicSubmit">提交</el-button>
               <el-button @click="$emit('update:catalogFormVisible', false)">取消</el-button>
             </div>
          </el-form>
   		</el-dialog>
    </div>
</template>
<script>
import _ from 'lodash'
export default{
	props:["addDIcTreeSubmit","updateDicDataSubmit","dictForm","catalogFormVisible","submitType"],
	data(){
		return{
      treeForm:{},
      rules:{
        attrName:[
            { required: true, message: '请输入字典目录', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        attrOrderId:[
            { required: true, message: '请输入排序值', trigger: 'blur' }
        ],
        attrId:[
            { required: true, message: '请输入唯一的標識', trigger: 'blur' }
        ],
        isParent:[
            { required: true, message: '請選擇是否為目錄', trigger: 'blur' }
        ]
      },
      title:''
		}
	},
  methods:{
    addDictionary(){
       this.dialogFormVisible = true
    },
    dicSubmit(){
      (this.treeForm.isParent == 1) ? this.treeForm.nodeFlag = 0 : this.treeForm.nodeFlag = 1;
      this.$refs['form'].validate((valid) => {
         if (valid) {
           if(this.submitType == "add"){
             console.log(this.treeForm);
             // this.treeForm.parentId    = this.did;
             this.addDIcTreeSubmit(this.treeForm);
           }else if(this.submitType == "update"){
             this.updateDicDataSubmit(this.treeForm);
           }
         }
         else{
             this.$message.error("请按规则填写");
         }
      })

    },
    addClose(){
        this.$emit('update:catalogFormVisible', false)
    }
  },
  created(){
     this.treeForm = _.clone(this.dictForm);
      if(this.submitType == "add"){
         this.title = "添加字典目錄";
      }else if(this.submitType == "update"){
         this.title = "修改字典目錄";
      }
  }
}
</script>
